package handlers

import (
	"coll-back/cache"
	"coll-back/models"
	"coll-back/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func LoginUser(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	var user models.User
	redis_key := "login_user_" + username
	errData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次，禁止登录",
		})
		return
	}
	err := models.DB.Where("username=?", username).Debug().First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询用户失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次，封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func CreateUserNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	nid, _ := strconv.Atoi(c.Query("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "关联失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
	})
	return
}
func ListUserRole(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		roles
		LEFT JOIN user_roles ON roles.id=user_roles.rid
	*/
	uid, _ := strconv.Atoi(c.Query("uid"))
	var role models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id=user_roles.rid").
		Where("user_roles.uid=?", uid).Debug().Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}
func ListRoleNode(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON nodes.id=role_nodes.rid
	*/
	rid, _ := strconv.Atoi(c.Query("rid"))
	var node models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.rid").
		Where("role_nodes.rid=?", rid).Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": node,
	})
	return
}
func CreateOrder(c *gin.Context) {
	depot := c.PostForm("depot")
	if depot == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.Depot{
		DepotName: depot,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CreateDepotOrder(c *gin.Context) {
	oid, _ := strconv.Atoi(c.PostForm("oid"))
	order_name := c.PostForm("order_name")
	if oid == 0 || order_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.DepotOrder{
		Oid:       oid,
		OrderName: order_name,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ListOrder(c *gin.Context) {
	var depot_order []models.DepotOrder
	redis_key := "list_order_"
	listData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if listData == "" {
		models.DB.Debug().Find(&depot_order)
		jsonData, _ := json.Marshal(&depot_order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(listData), &depot_order)
	}
	var total int64
	models.DB.Model(&depot_order).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表成功",
		"data":  depot_order,
		"total": total,
	})
	return
}
func DetailOrder(c *gin.Context) {
	id := c.Query("id")
	var depot_order []models.DepotOrder
	redis_key := "Detail_" + id

	dDetailData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if dDetailData == "" {
		models.DB.Where("id=?", id).Debug().Find(&depot_order)
		jsonData, _ := json.Marshal(&depot_order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(dDetailData), &depot_order)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表成功",
		"data": depot_order,
	})
	return
}
